/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.lacibMusic.upload;

import java.io.InputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

/**
 *
 * @author AlanmLira
 */
public class FTPUpload {

    @SuppressWarnings("CallToThreadDumpStack")
    public String uploadFTP(InputStream in, String tipo, String nome) {
        String hostName = "ftp.lacibufra.net";
        String username = "lacibufr";
        String password = "l@cbu3R1b";
        FTPClient ftp;
        String status = "erro";

        try {
            ftp = new FTPClient();
            ftp.connect(hostName);
            ftp.login(username, password);

            ftp.setFileType(FTP.BINARY_FILE_TYPE);
            switch (tipo) {
                case "Imagem":
                    ftp.changeWorkingDirectory("/public_html/lacibMusic/Imagem");
                    break;
                case "Audio":
                    ftp.changeWorkingDirectory("/public_html/lacibMusic/Audio");
                    break;
            }

            int reply = ftp.getReplyCode();
            System.out.println("Received Reply from FTP Connection:" + reply);

            if (FTPReply.isPositiveCompletion(reply)) {
                System.out.println("Connected Success");
            }
            switch (tipo) {
                case "Imagem":
                    ftp.storeFile(nome + ".png", in);
                    break;
                case "Audio":
                    ftp.storeFile(nome + ".mp3", in);
                    break;
            }

            status = "Sucesso";

            ftp.logout();
            ftp.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return status;
    }
}
